﻿/**             © 2008 Avanade Inc. All Rights Reserved.
 * 
 * Author:      Joris Valkonet, Avanade Netherlands
 * 
 * Content:     This class is the implementation of the gaussian RBF function algorithm
 *                 
 * */
using System;
using System.Collections.Generic;
using System.Text;

namespace Avanade.Datamining.SMO
{
    /**
     * The GaussianRBFKernel kernel : K(x, y) =  e^-(gamma * <x-y, x-y>^2)

     */
    public class GaussianRBFKernel : Kernel
    {
        private double[] kernelPrecalc;
        private double sigma = 1.0;

        public GaussianRBFKernel(Instances dataset, int cacheSize, double sigma)
            : base(dataset, cacheSize)
        {
            this.sigma = sigma;
            this.data = dataset;
        }

        protected override double evaluate(int id1, int id2, Instance inst1)
        {

            double precalc1;
            if (id1 == -1)
                precalc1 = dotProd(inst1, inst1);
            else
                precalc1 = kernelPrecalc[id1];
            Instance inst2 = data.instances[id2];
            double result = Math.Exp( (2d * dotProd(inst1, inst2) - precalc1 - kernelPrecalc[id2]) 
                                    / (2*sigma*sigma));
            return result;
        }

    }
}
